class Movie < ActiveRecord::Base

  validates_presence_of   :title, :description
  validates_uniqueness_of :title
  validates_length_of     :description, :minimum => 10
  validates_inclusion_of  :rating, :in => %w{ G PG PG-13 R NC-17 }

  has_many :reviews, :dependent => :destroy
  
  def self.find_all_appropriate_for_birthdate(birthdate)
    movies = Movie.find(:all)
    movies.select {|m| m.appropriate_for_birthdate?(birthdate) }
  end

  def appropriate_for_birthdate?(birthdate)
    if %w{ R NC-17 }.include?(rating)
      17.years.ago > birthdate
    elsif rating == "PG-13"
      13.years.ago > birthdate
    else
      true
    end
  end
  
end
